<?php

namespace App\System\Cis\Repositories;

use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Repositories\Repository;
use App\System\Cis\Model\CisModule;


class CisModuleRepository extends Repository
{
    use SinglePattern;

    /*
     * 模块列表
     * @return Collection
     * @auth Hjj
     */

    public function getlist($keyword = '')
    {
        $list = CisModule::where('parent_id', 0)
            ->when($keyword, function ($query) use ($keyword) {
                return $query->where('name', 'like', '%' . $keyword . '%');
            })
            ->get(['id', 'name', 'action_code', 'parent_id']);
        foreach ($list as $v) {
            $v->children = CisModule::where('parent_id', $v->id)->get(['id', 'name', 'action_code','parent_id']);
            foreach ($v->children as $cv){
                $cv->children = CisModule::where('parent_id',$cv->id)->get(['id','name','action_code','parent_id']);
            }
        }
        return $list;
    }

    public function addData($data)
    {
        $bool = CisModule::insertGetId($data);
        if ($bool) {
            return ['message' => '操作成功', 'data' => $bool, 'status' => 'SUCCESS'];
        } else {
            return ['message' => '操作失败', 'data' => ''];
        }
    }

    public function editData($id, $updata)
    {
        if (!$id) {
            return false;
        }
        $bool = CisModule::where('id', $id)->update($updata);
        if ($bool) {
            return ['message' => '操作成功', 'data' => $bool, 'status' => 'SUCCESS'];
        } else {
            return ['message' => '操作失败', 'data' => ''];
        }
    }

    public function getDetail($id)
    {
        if (!$id) {
            return false;
        }
        return CisModule::where('id', $id)->first();
    }

    public function delData($id)
    {
        if (!$id) {
            return false;
        }
        $bool = CisModule::where('id', $id)->delete();
        if ($bool) {
            return ['message' => '操作成功', 'data' => $bool, 'status' => 'SUCCESS'];
        } else {
            return ['message' => '操作失败', 'data' => ''];
        }
    }
}